/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_independent_pages.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_pmipreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn);

void
write_body_pmentryreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn);

int 
report_pages_most_ip_requested(GraphFrame *gf, char *name_file,int npages,
			    WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Requested Independent Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_pmipreq_html_file(gf,fp,npages,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_pmipreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn)
{
  LNode_type *lips = LOGR_get_list_independent_pages(msn);
  LNode_type *ptr = lips->back;
  WebIndependentPagePtr *varray = NULL;
  WebIndependentPagePtr *varray2 = NULL;
  int i=0,j=0;
  int tnpages=0;
  char ntitle[100];

  time_t now;

  long thits = LOGR_get_total_site_naccesses(msn);
  /*int tnops = lops ? count_llist(lops) : 0;*/
  int nhtml = 0;
  int nobj = 0;

  for(ptr = lips->back; ptr != lips; ptr=ptr->back)
  {
    WebIndependentPagePtr mip = (WebIndependentPagePtr)ptr->info;
    const char *mime = LOGR_get_ip_mime(mip);
    if(!mime)
	continue;
    if(!strcmp(mime,"www/unknown") || !strcmp(mime,"text/html"))
	nhtml++;
    else
	nobj++;
  }


  varray = nhtml ? (WebIndependentPagePtr *)calloc(nhtml,sizeof(WebIndependentPagePtr)) : NULL;

  if(!varray && nhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pmipreq_html_file\n");
      abort();
  }

  varray2 = nobj ? (WebIndependentPagePtr *)calloc(nobj,sizeof(WebIndependentPagePtr)) : NULL;

  if(!varray2 && nobj > 0) {
      fprintf(stderr,"Out of Memory write_body_pmipreq_html_file\n");
      abort();
  }

  for(ptr = lips->back; ptr != lips; ptr=ptr->back)
  {
    WebIndependentPagePtr mip = (WebIndependentPagePtr)ptr->info;
    const char *mime = LOGR_get_ip_mime(mip);
    if(!mime)
	continue;
    if(!strcmp(mime,"www/unknown") || !strcmp(mime,"text/html"))
	varray[i++] = mip;
    else
	varray2[j++] = mip;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Requested Independent Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVIPString); 
  write_horizontal_rule_html_file(fp);

  if(nhtml > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Independent HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Hits","Percentage","Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebIndependentPagePtr mip = (WebIndependentPagePtr)
	randomized_select((GPTR *)varray,i-1,nhtml-1,1,
			  LOGR_is_access_ip_lower,LOGR_is_access_ip_bigger);
    if(mip)
    {
      const char *url = LOGR_get_ip_url(mip);
      long nacc = LOGR_get_ip_naccesses(mip);
      long sbytes = LOGR_get_ip_sizebytes(mip);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      write_row_num_link_string3(fp,snum,url,scount,save,ssbytes);
    }	
  }
  write_end_table_html_file(fp); }

  if(nobj > 0) {
  if(npages > nobj)
      tnpages = nobj;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Independent Non HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Hits","Percentage","Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebIndependentPagePtr mip = (WebIndependentPagePtr)
	randomized_select((GPTR *)varray2,i-1,nobj-1,1,
			  LOGR_is_access_ip_lower,LOGR_is_access_ip_bigger);
    if(mip)
    {
      const char *url = LOGR_get_ip_url(mip);
      long nacc = LOGR_get_ip_naccesses(mip);
      long sbytes = LOGR_get_ip_sizebytes(mip);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      write_row_num_link_string3(fp,snum,url,scount,save,ssbytes);
    }	
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
}

int 
report_entry_pages_most_requested(GraphFrame *gf, char *name_file,int npages,
				  WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Requested Entry Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_pmentryreq_html_file(gf,fp,npages,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_pmentryreq_html_file(GraphFrame *gf,FILE *fp,int npages,
				WebStatPtr msn)
{
  LNode_type *lentrys = LOGR_get_list_entry_pages(msn);
  LNode_type *ptr = NULL;
  WebGenPagePtr *varray = NULL;
  WebGenPagePtr *varray2 = NULL;
  int i=0,j=0;
  int tnpages=0;
  char ntitle[100];
  char *scolor = "beige";

  time_t now;

  long thits = LOGR_get_total_site_naccesses(msn);

  /*int tnentrys = lentrys ? count_llist(lentrys) : 0;*/

  int tnhtml = 0, tnnhtml=0;

  for(ptr = lentrys->back; ptr != lentrys; ptr=ptr->back)
  {
    WebGenPagePtr mip = (WebGenPagePtr)ptr->info;
    const char *mime = LOGR_get_gen_mime(mip);
    if(mime)
    {
	if(!strcmp(mime,"text/html") || !strcmp(mime,"www/unknown"))
	    tnhtml++;
	else
	    tnnhtml++;
    }
  }

  varray = tnhtml ? (WebGenPagePtr *)calloc(tnhtml,sizeof(WebGenPagePtr)) : NULL;

  if(!varray && tnhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pmentryreq_html_file\n");
      abort();
  }

  varray2 = tnnhtml ? (WebGenPagePtr *)calloc(tnnhtml,sizeof(WebGenPagePtr)) : NULL;

  if(!varray2 && tnnhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pmentryreq_html_file\n");
      abort();
  }

  for(ptr = lentrys->back; ptr != lentrys; ptr=ptr->back)
  {
    WebGenPagePtr mip = (WebGenPagePtr)ptr->info;
    const char *mime = LOGR_get_gen_mime(mip);
    if(mime)
    {
	if(!strcmp(mime,"text/html") || !strcmp(mime,"www/unknown"))
	    varray[i++] = mip;
	else
	    varray2[j++] = mip;
    }
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Requested Entry Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RMVEPString); 
  write_horizontal_rule_html_file(fp);

  if(tnhtml > 0) {
  if(npages > tnhtml)
      tnpages = tnhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested HTML Entry Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string8(fp,"Num", "URL","Entry Hits","Percentage",
			    "No Referer", "Dynamic Referer", "Static Referer",
			    "Total Hits", "Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebGenPagePtr mip = (WebGenPagePtr)
	randomized_select((GPTR *)varray,i-1,tnhtml-1,1,
			  LOGR_is_tot_access_gen_lower,
			  LOGR_is_tot_access_gen_bigger);
    if(mip)
    {
      const char *url = LOGR_get_gen_url(mip);
      long sbytes = LOGR_get_gen_sizebytes(mip);
      long cntnoref = LOGR_get_gen_naccesses(mip);
      long cntstaref = LOGR_get_gen_nacc_static(mip);
      long cntdynref = LOGR_get_gen_nacc_dynamic(mip);
      long nacc = cntnoref + cntstaref + cntdynref;
      long totcount = LOGR_get_gen_tot_naccesses(mip);
      char *tit = (char *)LOGR_get_gen_title(mip);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      char scntnoref[20],scntstaref[20],scntdynref[20],stotcount[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scntnoref,"%ld",cntnoref);
      sprintf(scntstaref,"%ld",cntstaref);
      sprintf(scntdynref ,"%ld",cntdynref);
      sprintf(stotcount,"%ld",totcount);
      if(LOGR_is_gen_page_independent(mip))
	  write_row_num_link_title_string7_color(fp,snum,url,tit,scount,save,
					   scntnoref, scntdynref,scntstaref,
					   stotcount,ssbytes,scolor);
      else
	  write_row_num_link_title_string7_color(fp,snum,url,tit,scount,save,
					   scntnoref, scntdynref,scntstaref,
					   stotcount,ssbytes,NULL);

    }	
  }
  write_end_table_html_file(fp); }

  if(tnnhtml > 0) {
  if(npages > tnnhtml)
      tnpages = tnnhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Non HTML Entry Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string8(fp,"Num", "URL","Entry Hits","Percentage",
			    "No Referer", "Dynamic Referer", "Static Referer",
			    "Total Hits", "Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebGenPagePtr mip = (WebGenPagePtr)
	randomized_select((GPTR *)varray2,i-1,tnnhtml-1,1,
			  LOGR_is_tot_access_gen_lower,
			  LOGR_is_tot_access_gen_bigger);
    if(mip)
    {
      const char *url = LOGR_get_gen_url(mip);
      long sbytes = LOGR_get_gen_sizebytes(mip);
      long cntnoref = LOGR_get_gen_naccesses(mip);
      long cntstaref = LOGR_get_gen_nacc_static(mip);
      long cntdynref = LOGR_get_gen_nacc_dynamic(mip);
      long nacc = cntnoref + cntstaref + cntdynref;
      long totcount = LOGR_get_gen_tot_naccesses(mip);
      char *tit = (char *)LOGR_get_gen_title(mip);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      char scntnoref[20],scntstaref[20],scntdynref[20],stotcount[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      sprintf(scntnoref,"%ld",cntnoref);
      sprintf(scntstaref,"%ld",cntstaref);
      sprintf(scntdynref ,"%ld",cntdynref);
      sprintf(stotcount,"%ld",totcount);
      if(LOGR_is_gen_page_independent(mip))
	  write_row_num_link_title_string7_color(fp,snum,url,tit,scount,save,
					   scntnoref, scntdynref,scntstaref,
						 stotcount,ssbytes,scolor);
      else
	  write_row_num_link_title_string7_color(fp,snum,url,tit,scount,save,
					   scntnoref, scntdynref,scntstaref,
						 stotcount,ssbytes,NULL);
    }	
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
}


